// pages/addRedpag/index.js
const app = getApp()
var rid;
var uid;
var cashRatio = 0.02;
var cashLow = 2;
var minNum = 2;
var maxNum = 99;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bagImg: [
      '/images/icon/redicon.png',
    ],
    disabled:false,
    amount: 0.00,
    fee: 0.00,
    userBasic: {},
    fromData:'',
  },
  goEditCradInfo:function(){
    wx.navigateTo({
      url: '../editCardInfo/index',
    })
  },
  goExplain:function(){
    wx.navigateTo({
      url: '../explain/index',
    })
  },
  goLetRedpag:function(){
    wx.navigateTo({
      url: '../letRedpag/index',
    })
  },
  goVisitorInfo:function(){
    var that = this;
    wx.switchTab({
      url: '../userInfo/index',
      success: function(res) {
      },
      fail: function(res) {
        console.log(res)
      },
    })
  },
  //获取个人基本信息
  getUserBasicInfo: function () {
    var that = this;
    var data = [];
    var url = 'We_Community/getBrowsePersonalUserBasic'
    var calkdata = app.netWorkRequest(that, data, url, 'userBasic')
  },
  baseImage: function () {
    var that = this;
    var data = that.data.bagImg;
    console.log('转码', data)
    for (var i = 0; i < data.length; i++) {
      var bgm = app.baseImage(data[i]);
      data[i] = bgm;
    }
    that.setData({
      bagImg: data
    })
  },
  testAmount: function (e) {
    var that = this;
    var amount = e.detail.value;
    var d = that.data;
    var quota = d.userBasic['Smallchange'];
    var t = '手续费';
    var fee = amount * (cashRatio * 1);
    fee = fee.toFixed(2);
    amount = (amount * 1 + fee * 1);
    console.log('testAmountt事件，携带数据为：', fee)
    that.setData({
      fee: fee,
      amount: amount
    })
  },
  initData: function () {
    this.setData({
      disabled:false,
      amount: 0.00,
      fee: 0.00,
      fromData: '',
    })
  },
  sendCreateRedpag:function(e){
    var that = this;
    if (that.data.userBasic['Integrity'] != true)
      return app.promptModal('请先完善名片信息');
    var dt = e.detail.value;
    var amount = this.data.amount*1;
    console.log('form发生了submit事件，携带数据为：', dt)
    if (!dt.amount || dt.amount == '' || dt.amount < cashLow) 
    return app.promptModal('请输入正确的红包金额');
    if (!dt.num || dt.num == '' || dt.num < minNum || dt.num > maxNum) 
    return app.promptModal('请输入正确的红包数量');
    var type = 1;
    if (dt.switch == true)
    type = 2;
    var data = [];
    that.setData({
      disabled: true,
    })
    data['amt'] = dt.amount;
    data['num'] = dt.num;
    data['msg'] = dt.message;
    data['type'] = type;
    var url = 'We_Redenvelopes/getRandomRedenvelopePayInfo'
    var calkdata = app.netWorkRequest(that, data, url, 'wxpay')
  },

   /**
   * 微信支付
   */

  paymentTransfer: function (data) {
    var that = this;
    //js支付
    var timeStamp = data.timeStamp;
    var nonceStr = data.nonceStr;
    var pagid = data.package;
    var paySign = data.paySign;
    rid = data.red_id;
    uid = data.uid;
    console.log(rid)
    wx.requestPayment({
      'timeStamp': timeStamp,
      'nonceStr': nonceStr,
      'package': pagid,
      'signType': 'MD5',
      'paySign': paySign,
      success: function (res) {
        //that.initData();
        if (res.errMsg == 'requestPayment:ok') {
          wx.navigateTo({
            url: '../shareOne/index?rid=' + rid,
          })
        } else {
          app.prompt('支付失败')
        }
      },
      fail: function (res) {
        app.prompt('支付失败')
      },
      complete:function(res){
        that.initData();
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        wx.hideShareMenu();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.baseImage();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserBasicInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})